package cc.vv.baselibrary.util;

import java.util.HashMap;

import cc.vv.BaseNewApplication;
import cc.vv.baselibrary.R;
import cc.vv.baselibrary.bean.base.BaseEntityObj;
import cc.vv.baselibrary.bean.base.BaseResponseObj;
import cc.vv.baselibrary.vFinal.BTCodeKey;
import cc.vv.lkbasecomponent.http.LKHttp;
import cc.vv.lkbasecomponent.http.lib.CallBack;
import cc.vv.lkbasecomponent.util.LKStringUtil;

/**
 * token 管理工具类
 *
 * @author dupeng dupeng@vv.cc
 * @date 2018/4/8
 */

public class TokenUtil {


    private static TokenUtil instance;

    public static TokenUtil getInstance() {
        if (instance == null) {
            synchronized (TokenUtil.class) {
                if (instance == null) {
                    instance = new TokenUtil();
                }
            }
        }
        return instance;
    }

    /**
     * token 处理.
     *
     * @param code
     * @return
     */
    public boolean dealBadToken(int code) {
        switch (code) {
            case BTCodeKey.CODE_200001:
                //AccessToken已被注销，请重新登录
                LKToastUtil.showToastShort(LKStringUtil.getString(R.string.str_login_invalid));
                AppManager.getInstance().loginOut(BaseNewApplication.getApplication());
                break;
            case BTCodeKey.CODE_200002:
                //AccessToken为空，请检查参数
                // 刷新token
                refreshAccessToken();
                break;
            case BTCodeKey.CODE_200003:
                //AccessToken已过期
                // 刷新token
                refreshAccessToken();
                break;
            case BTCodeKey.CODE_200004:
                //RefreshToken已过期
                LKToastUtil.showToastShort(LKStringUtil.getString(R.string.str_login_invalid));
                AppManager.getInstance().loginOut(BaseNewApplication.getApplication());
                break;
            case BTCodeKey.CODE_200005:
                //token类型错误，请使用accessToken
                break;
            case BTCodeKey.CODE_200006:
                //token类型错误，请使用refreshToken
                break;
            default:
                return false;
        }
        return true;
    }

    /**
     * 请求token刷新接口
     */
    public void refreshAccessToken() {
        LKHttp.post("刷新token的url", new HashMap<>(1), TokenResponseBean.class, new CallBack<TokenResponseBean>() {
            @Override
            public void onSuccess(String url, TokenResponseBean obj) {
                super.onSuccess(url, obj);
                if (obj != null && obj.code == BTCodeKey.CODE_200) {
                    //请求成功
                    setAccessToken(obj.data.accessToken);
                    setRefreshToken(obj.data.refreshToken);
                }
            }
        }, false);
    }

    /**
     * 设置accessToken 数据
     *
     * @param refreshToken
     */
    public void setRefreshToken(String refreshToken) {
    }


    /**
     * 设置accessToken 数据
     *
     * @param accessToken
     */
    public void setAccessToken(String accessToken) {


    }

    /**
     * 获取accessToken 数据
     *
     * @return
     */
    public String getAccessToken() {
        return "token";
    }

    /**
     * 获取刷新Token 数据
     *
     * @return
     */
    public String getRefreshToken() {
        return "new_token";
    }

    private class TokenResponseBean extends BaseResponseObj<TokenBean> {
    }

    private class TokenBean extends BaseEntityObj {
        public String accessToken;
        public String refreshToken;
    }
}
